package com.it;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.wxpay.sdk.WXPay;
import com.it.bean.R;
import com.it.bean.RestStatusEnum;
import com.it.config.MyPayConfig;
import com.it.dao.OrderDetailMapper;
import com.it.dao.OrderMapper;
import com.it.pojo.Order;
import com.it.pojo.OrderDetail;
import com.it.pojo.params.order.OrderInfoQuery;
import com.it.pojo.params.order.OrderQuery;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("order")
@CrossOrigin
@Api(value = "v1", tags = "订单相关接口")

public class OrderController {

    @Resource
    private OrderService orderService;

    @Resource
    private OrderDetailMapper orderDetailMapper;

    @PostMapping("save")
    public R<?> save(@RequestBody OrderQuery orderQuery) throws Exception {
        if (orderQuery == null) {
            return R.error(RestStatusEnum.PARAMS_ERROR);
        }
        System.out.println("orderQuery = " + orderQuery);
        Map<String, String> map = orderService.saveOrder(orderQuery);
        String orderId = map.get("orderId");
        System.out.println("map.get(\"orderId\") = " + map.get("orderId"));
        LambdaQueryWrapper<OrderDetail> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(OrderDetail::getOrderId, Long.parseLong(orderId));
        OrderDetail orderDetail = orderDetailMapper.selectOne(wrapper);

        if (orderId != null) {
            WXPay wxPay = new WXPay(new MyPayConfig());
            HashMap<String, String> data = new HashMap<>();
            data.put("body", orderDetail.getProductName());  //商品描述
            data.put("description", "商品描述");
            data.put("out_trade_no", orderId);               //使用当前用户订单的编号作为当前支付交易的交易号
            data.put("fee_type", "CNY");      //支付币种
            data.put("total_fee", "1");
            data.put("trade_type", "NATIVE");                //交易类型
            data.put("notify_url", "http://81.68.105.218:8888//pay/callback");  // 设置支付完成时的回调方法接口
            //            发送请求，获取响应
            Map<String, String> resp = wxPay.unifiedOrder(data);
            map.put("payUrl", resp.get("code_url"));
            resp.put("orderId", orderId);
            System.out.println("resp = " + resp);
            return R.success("成功", resp);
        } else {
            return R.error();
        }
//        保存订单的同时，向微信支付平台发起请求

    }

    @GetMapping("status")
    public R<?> update(@RequestParam("orderId") Long orderId) {
        if (orderId == null) {
            return R.error(RestStatusEnum.PARAMS_ERROR);
        }
        int i = orderService.getOrderStatus(orderId);
        return i > 0 ? R.success("支付成功", 1) : R.success("支付失败", 0); // 判断如果==1 的话就支付成功
    }


    @PostMapping("")
    public R<?> getOrderInfo(@RequestBody OrderInfoQuery orderInfoQuery) {
        if (orderInfoQuery == null) {
            return R.error(RestStatusEnum.PARAMS_ERROR);
        }
        System.out.println("orderInfoQuery = " + orderInfoQuery);
        Map<String, Object> map = orderService.listOrder(orderInfoQuery);
        return R.success("成功", map);
    }
}
